package com.cxf.windex.mina;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.WxUtil;
import com.cxf.windex.biz.CxfUserBiz;
import com.cxf.windex.entity.CxfUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: drp
 * @description: 公众号/小程序专用接口
 * @author: chenxf
 * @create: 2018-06-04 16:04
 **/
@Api(tags = {"聚能小宝 - 公众号微信 接口"})
@Controller
@RequestMapping("mina")
public class MinaController extends BaseRestController<CxfUserBiz, CxfUser> {


    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.secret}")
    private String secret;

    @ApiOperation(value = "获取微信签名", notes = "获取微信签名")
    @RequestMapping(value = "sign", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse sign(String url) {
        return new ObjectRestResponse().rel(true).result(
                WxUtil.sign(appid, secret, url));
    }

    @ApiOperation(value = "获取微信openid", notes = "获取微信openid")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse login(String code) {
        return new ObjectRestResponse().rel(true).result(WxUtil.getOpenId(appid, secret, code));
    }

    @ApiOperation(value = "登陆获取用户信息", notes = "登陆获取用户信息，如未注册返回微信详细信息，包括头像")
    @RequestMapping(value = "xcxLogin", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse xcxLogin(String unionid) {
        CxfUser cxfUser = new CxfUser();
        cxfUser.setWxopenid(unionid);
        cxfUser = baseBiz.selectOne(cxfUser);
        if (cxfUser == null) {
            //获取微信 openid详细信息

            return new ObjectRestResponse().rel(false).result(WxUtil.getOpenIdDetail(appid,secret,unionid));
        }
        resultMap.put("token", this.setCurrentUserName(cxfUser));

        return new ObjectRestResponse().rel(true).msg("登陆成功").result(resultMap);

    }
}
